﻿namespace MyLibrary.API.Data
{
    using System;
    using System.Text;
    using System.Data;
    using System.Data.Common;
    using System.Data.OleDb;

    public class OleDbDatabase: DbDatabase
    {
        public OleDbDatabase(string connectionString) :
            base(connectionString) { }

        public override DbDataReader ExecuteReader(string query)
        {
            OleDbConnection connection = new OleDbConnection(this.connectionString);
            OleDbCommand command = new OleDbCommand(query, connection);            

            connection.Open();
            return (DbDataReader)command.ExecuteReader(CommandBehavior.CloseConnection);
        }

        public override DbDataReader ExecuteReader(string query, MyLibrary.API.Core.Param<object>[] Parameters)
        {
            OleDbConnection connection = new OleDbConnection(this.connectionString);
            OleDbCommand command = new OleDbCommand(query, connection);

            foreach (var param in Parameters)
            {
                command.Parameters.AddWithValue(param.Name, param.Value);
            }

            connection.Open();
            return (DbDataReader)command.ExecuteReader(CommandBehavior.CloseConnection);
        }

        public override int ExecuteNonQuery(string query)
        {
            OleDbConnection conneciton = new OleDbConnection(this.connectionString);
            OleDbCommand command = new OleDbCommand(query, conneciton);

            conneciton.Open();
            int rowsAffected = command.ExecuteNonQuery();
            conneciton.Close();
            return rowsAffected;
        }

        public override int ExecuteNonQuery(string query, params MyLibrary.API.Core.Param<object>[] Parameters)
        {
            OleDbConnection conneciton = new OleDbConnection(this.connectionString);
            OleDbCommand command = new OleDbCommand(query, conneciton);

            foreach (var param in Parameters)
            {
                command.Parameters.AddWithValue(param.Name, param.Value);
            }

            conneciton.Open();
            int rowsAffected = command.ExecuteNonQuery();
            conneciton.Close();
            return rowsAffected;            
        }

        public override object ExecuteScalar(string query)
        {
            OleDbConnection conneciton = new OleDbConnection(this.connectionString);
            OleDbCommand command = new OleDbCommand(query, conneciton);

            conneciton.Open();
            object value = command.ExecuteScalar();
            conneciton.Close();
            return value;
        }

        public override object ExecuteScalar(string query, params MyLibrary.API.Core.Param<object>[] Parameters)
        {
            OleDbConnection conneciton = new OleDbConnection(this.connectionString);
            OleDbCommand command = new OleDbCommand(query, conneciton);

            foreach (var param in Parameters)
            {
                command.Parameters.AddWithValue(param.Name, param.Value);
            }

            conneciton.Open();
            object value = command.ExecuteScalar();
            conneciton.Close();
            return value;
        }
    }
}
